﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controller;
using SBS;

namespace GUI
{
    public partial class OpretScooter : DesignChildForm
    {
        public OpretScooter(string regNr)
        {
            InitializeComponent();
            if (regNr != "")
            {
                RegNrTextBox.ReadOnly = true;
                RegNrTextBox.Text = regNr;
                GemScooterButton.Text = "Opdater";
                this.toolStripLabel1.Text = "Rediger scooter";

            }
            this.toolStripLabel1.Text = "Opret scooter";

           
        }

        private void OpretScooter_Load(object sender, EventArgs e)
        {
            Vaerksted v = Vaerksted.GetInstance();
            MaerkeComboBox.ValueMember = "Key";
            MaerkeComboBox.DisplayMember = "Value";
            MaerkeComboBox.DataSource = v.HentCertifikatListe();
            
            if(RegNrTextBox.Text.Length >0)
                HentScooterInfo(RegNrTextBox.Text);
        }

        private void GemScooterButton_Click(object sender, EventArgs e)
        {
            Vaerksted v = Vaerksted.GetInstance();

            int Knr = v.Kkatalog.AktivKunde.Knr;

            if (Valider.KunTalOgBogstaver(RegNrTextBox.Text) && Valider.KunHelTal(AargangTextBox.Text) && Valider.KunDecimalTal(KmTextBox.Text)) 
            {

                string Regnr = RegNrTextBox.Text;
                int Certnr = Convert.ToInt32(MaerkeComboBox.SelectedValue);
                string Model = ModelTextBox.Text;
                int Aar = Convert.ToInt32(AargangTextBox.Text);
                double Antkm = Convert.ToUInt32(KmTextBox.Text);

                if (v.Kkatalog.AktivKunde.Scootere.ContainsKey(Regnr))
                {
                    v.Kkatalog.AktivKunde.Scootere[Regnr].AntKm = Antkm;
                    v.Kkatalog.AktivKunde.Scootere[Regnr].Aar = Aar;
                    v.Kkatalog.AktivKunde.Scootere[Regnr].Certifikat.CertNr = Certnr;
                    v.Kkatalog.AktivKunde.Scootere[Regnr].Model = Model;
                    v.Kkatalog.AktivKunde.Scootere[Regnr].Certifikat.CertNavn = MaerkeComboBox.Text;


                }
                else
                {
                    Scooter s = new Scooter(Regnr, DateTime.Now, Aar, Antkm, Certnr, MaerkeComboBox.Text, Model);
                    s.SidsteService = DateTime.Today.Date;


                    v.Kkatalog.AktivKunde.Scootere.Add(s.RegNr, s);
                }
                Close();
                
            }
            else
            {
            MessageBox.Show("Der var en eller flere fejl i informationen");
            }
        }

        private void MaerkeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void HentScooterInfo(string Regnr)
        {
            Vaerksted v = Vaerksted.GetInstance();
            MaerkeComboBox.SelectedValue = v.Kkatalog.AktivKunde.Scootere[Regnr].Certifikat.CertNr;
            ModelTextBox.Text = v.Kkatalog.AktivKunde.Scootere[Regnr].Model;
            AargangTextBox.Text = v.Kkatalog.AktivKunde.Scootere[Regnr].Aar.ToString();
            KmTextBox.Text = v.Kkatalog.AktivKunde.Scootere[Regnr].AntKm.ToString();
            KundeNummerTextBox.Text = v.Kkatalog.AktivKunde.Knr.ToString();
        }

        private void AnnullerButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
